package com.zwr.normal.sharemodel.aba;

import java.util.concurrent.atomic.AtomicReference;

public class ABAProblem {
    static AtomicReference<String> ref = new AtomicReference<>("A");

    public static void main(String[] args) throws Exception {
        System.out.println("main start ... ");
        String pre = ref.get();

        other();
        Thread.sleep(2000);

        System.out.println("main continue...");

        ref.compareAndSet(pre,"C");
        String next = ref.get();
        System.out.println(pre+"--->"+next);
    }

    private static void other() throws InterruptedException {
        new Thread(()->{
            String pre = ref.get();
            ref.compareAndSet(ref.get(),"B");
            String next = ref.get();
            System.out.println(pre+"--->"+next);
        }).start();

        Thread.sleep(1000);

        new Thread(()->{
            String pre = ref.get();
            ref.compareAndSet(ref.get(),"A");
            String next = ref.get();
            System.out.println(pre+"--->"+next);
        }).start();
    }
}
